package com.bw.controller;

import com.alibaba.fastjson.JSON;
import com.bw.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

@Controller
public class HelloController {


    /**
     * 用来接收客户端的请求
     * @return
     */
    @RequestMapping("/hello.do") // 定义请求路径，路径全局不能重复
    public String hello(Model model, HttpServletRequest request){

        String name = request.getParameter("name");
        System.out.println("name===" + name);
        String password = request.getParameter("password");
        System.out.println("password===" + password);

        String msg = "hello controller 返回了这个页面";
        // 往jsp页面赋值
        model.addAttribute("msg",msg);
        model.addAttribute("list", Arrays.asList("1","2","3"));
        // 返回 jsp页面的名字
        return "hello";
    }

    /**
     * 参数绑定
     * 入参数的名字 和方法定义的形参的名字保持一致
     * 将URL中的的请求参数，进行类型转换（String或其他类型），将转换后的值在赋值给Controller方法形参中
     * @param model
     * @param name
     * @param password
     * @return
     */
    @RequestMapping("/hello2.do")
    public String hello2(Model model,String name, @RequestParam(value = "pwd",required = false,defaultValue = "66666") Integer password){

        System.out.println("name===" + name);
        System.out.println("password===" + password);
        String msg = "hello2 controller 返回了这个页面";
        // 往jsp页面赋值
        model.addAttribute("msg",msg);
        return "hello";
    }

    /**
     * 定义一个对象   来接收参数
     * @param model
     * @return
     */
    @RequestMapping("/hello3.do")
    public String hello3(Model model, User user){

        System.out.println("user = " + JSON.toJSONString(user));
        String msg = "hello3 controller 返回了这个页面";
        // 往jsp页面赋值
        model.addAttribute("msg",msg);
        return "hello";
    }


}
